package com.fr.report.io;

import com.fr.base.FRContext;
import com.fr.base.core.BaseCoreUtils;
import com.fr.report.PageSet;
import com.fr.report.PageSetChain;
import com.fr.report.Report;
import com.fr.report.ReportPage;
import com.fr.report.ReportSettings;
import com.fr.report.ResultWorkBook;
import com.fr.report.core.DynamicValueList;
import com.fr.report.core.ReportHelper;
import com.fr.report.core.ReportUtils;
import com.fr.report.io.pdf.MyFontMapper;
import com.fr.report.painter.PagePainter;
import com.fr.third.com.lowagie.text.Document;
import com.fr.third.com.lowagie.text.Rectangle;
import com.fr.third.com.lowagie.text.pdf.BaseFont;
import com.fr.third.com.lowagie.text.pdf.PdfContentByte;
import com.fr.third.com.lowagie.text.pdf.PdfWriter;
import com.fr.util.OperatingSystem;
import com.fr.util.Utils;
import java.awt.Graphics2D;
import java.io.File;
import java.io.OutputStream;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PDFExporter extends AbstractAppExporter
{
  public static final String TYPE_ORIGINAL = "ori";
  public static final String TYPE_RESOLVESOMETHING = "rs";
  protected boolean isPrint;
  protected static MyFontMapper fontMapper = null;

  public PDFExporter()
  {
    this(false);
  }

  public PDFExporter(boolean paramBoolean)
  {
    this.isPrint = false;
    this.isPrint = paramBoolean;
  }

  public void export(OutputStream paramOutputStream, ResultWorkBook paramResultWorkBook)
    throws Exception
  {
    PageSet localPageSet = paramResultWorkBook.generateReportPageSet(ReportUtils.getPaperSettingListFromWorkBook(paramResultWorkBook)).getTraversingCase();
    export(paramOutputStream, localPageSet);
    localPageSet.release();
  }

  public void export(OutputStream paramOutputStream, PageSet paramPageSet)
    throws Exception
  {
    Document localDocument = null;
    PdfWriter localPdfWriter = null;
    int i = 1;
    for (int j = 0; j < paramPageSet.size(); ++j)
    {
      ReportPage localReportPage = paramPageSet.getPage(j);
      if (localReportPage == null)
        break;
      float f1 = 0.75F;
      float f2 = localReportPage.getPageWidth() * f1;
      float f3 = localReportPage.getPageHeight() * f1;
      if (localDocument == null)
      {
        localDocument = new Document(new Rectangle(f2, f3));
        localPdfWriter = PdfWriter.getInstance(localDocument, paramOutputStream);
        localDocument.open();
      }
      if (i == 0)
      {
        localDocument.setPageSize(new Rectangle(f2, f3));
        localDocument.newPage();
      }
      i = 0;
      PdfContentByte localPdfContentByte = localPdfWriter.getDirectContent();
      Graphics2D localGraphics2D = localPdfContentByte.createGraphics(f2, f3, prepareFontMapper());
      if (!(this.isPrint))
        localGraphics2D.scale(f1, f1);
      if (localReportPage.getReportSettings().isLonelyPage())
      {
        Report localReport = localReportPage.getSourceReport();
        DynamicValueList localDynamicValueList1 = ReportHelper.getColumnWidthList(localReport);
        DynamicValueList localDynamicValueList2 = ReportHelper.getRowHeightList(localReport);
        int k = localReport.getColumnCount();
        int l = localReport.getRowCount();
        int i1 = 0;
        int i2 = 0;
        for (int i3 = 0; i3 < k; ++i3)
          i1 += localDynamicValueList1.get(i3);
        for (i3 = 0; i3 < l; ++i3)
          i2 += localDynamicValueList2.get(i3);
        double d1 = localReportPage.getPageWidth() / (localReportPage.getMarginLeft() + localReportPage.getMarginRight() + i1);
        double d2 = localReportPage.getPageHeight() / (i2 + localReportPage.getMarginTop() + localReportPage.getMarginBottom());
        d1 = (d1 < 1.0D) ? d1 : 1.0D;
        d2 = (d2 < 1.0D) ? d2 : 1.0D;
        localGraphics2D.scale(d1, d2);
      }
      new PagePainter(localReportPage, localGraphics2D, this.isPrint).convert();
      localGraphics2D.dispose();
    }
    if (localDocument != null)
      localDocument.close();
  }

  protected static MyFontMapper prepareFontMapper()
  {
    if (fontMapper != null)
      return fontMapper;
    fontMapper = new MyFontMapper();
    try
    {
      if (OperatingSystem.isWindows())
      {
        File localFile;
        String str1 = System.getProperty("java.library.path");
        String[] arrayOfString = Utils.splitString(str1, ";");
        for (int i = 0; i < arrayOfString.length; ++i)
        {
          localFile = new File(arrayOfString[i]);
          InsertDirectory(fontMapper, new File(localFile, "Fonts"));
        }
        InsertDirectory(fontMapper, new File("C:\\WINNT\\Fonts"));
        InsertDirectory(fontMapper, new File("C:\\WINDOWS\\Fonts"));
        String str2 = System.getProperty("java.home");
        if (str2 != null)
        {
          localFile = new File(BaseCoreUtils.pathJoin(new String[] { str2, "lib", "fonts" }));
          if ((localFile.exists()) && (localFile.isDirectory()))
            InsertDirectory(fontMapper, localFile);
        }
      }
      else
      {
        InsertDirectory(fontMapper, new File("/usr/X11R6/lib/X11/fonts"));
        InsertDirectory(fontMapper, new File("/usr/share/fonts"));
        InsertDirectory(fontMapper, new File("/usr/X/lib/X11/fonts/TrueType"));
        InsertDirectory(fontMapper, new File("/usr/openwin/lib/X11/fonts/TrueType"));
      }
      if ((FRContext.getLocale() == Locale.CHINA) || (FRContext.getLocale() == Locale.CHINESE))
        MyFontMapper.defaultFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
      else if ((FRContext.getLocale() == Locale.TAIWAN) || (FRContext.getLocale() == Locale.TRADITIONAL_CHINESE))
        MyFontMapper.defaultFont = BaseFont.createFont("MSung-Light", "UniCNS-UCS2-H", false);
      else if ((FRContext.getLocale() == Locale.JAPAN) || (FRContext.getLocale() == Locale.JAPANESE))
        MyFontMapper.defaultFont = BaseFont.createFont("HeiseiKakuGo-W5", "UniJIS-UCS2-H", false);
      else if ((FRContext.getLocale() == Locale.KOREA) || (FRContext.getLocale() == Locale.KOREAN))
        MyFontMapper.defaultFont = BaseFont.createFont("HYGoThic-Medium", "UniKS-UCS2-H", false);
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    return fontMapper;
  }

  protected static void InsertDirectory(MyFontMapper paramMyFontMapper, File paramFile)
  {
    if ((paramFile == null) || (!(paramFile.exists())) || (!(paramFile.isDirectory())))
      return;
    paramMyFontMapper.insertDirectory(paramFile.getAbsolutePath());
    File[] arrayOfFile = paramFile.listFiles();
    for (int i = 0; i < arrayOfFile.length; ++i)
      InsertDirectory(paramMyFontMapper, arrayOfFile[i]);
  }
}